# controlls the display and editing of team/buglist projections

class ProjectionsController < ApplicationController
  layout 'admin_standard'
  before_filter :authenticate

  ##
  # Show the projections for a given team
  def index_for_team
    @team = Team.find(params[:id])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @projections }
    end 
  end

  ##
  # Show the projections for a given bug list
  def index_for_bug_list
    @bug_list = BugList.find(params[:id])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @projections }
    end 
  end 

  ##
  # Show the projections for a given team and bug list.
  # Expects to recieve both a team and a bug list.
  def show
    
    @team = Team.find(params[:team_id])
    @bug_list = BugList.find(params[:bug_list_id])
    @projections = Projection.find_projections_by_team_and_bug_list(@team, @bug_list)
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @projection }
    end
  end



  ##
  # Display the edit page for projections for the given team and bug list
  # will only provide the edit page if the current logged in user is permitted
  # to edit these projections.
  def edit_group
    #Projection.find(:all, :conditions => {:team_id => params[:team_id], :bug_list_id => params[:bug_list_id] } )
     @team = Team.find(params[:team_id])
     @bug_list = BugList.find(params[:bug_list_id])
     @projections = Projection.find_projections_by_team_and_bug_list(@team, @bug_list)
     @projection = Projection.new
     
     if @team.manager.username == session[:username] || @team.project.is_logged_in_project_administrator?
       # there's nothing to see here. this person is allowed. keep going

       ## find the actual projection dates. These may have changed 
       ## if the project start date or projection weeks were changed.
      respond_to do |format|
       format.html # edit.html.erb
       format.xml  { render :xml => @projection }
      end

     else
          flash[:notice] = "Stop messing with other team leads projections. It's rude."
          redirect_to( :controller => :projections, :id => @team.id, :bug_list_id => @bug_list.id, :team_id => @team.id, :action => :show )
     end
     


  end



  ##
  # Save the projections for the given team and bug list
  # will only provide the save if the current logged in user is permitted
  # to edit these projections.
  def save_group
    @team = Team.find(params[:projection][:team_id])
    @bug_list = BugList.find(params[:projection][:bug_list_id])
    
    if @team.manager.username == session[:username] || @team.project.is_logged_in_project_administrator?
    
        logger.info "saving projections"
        #logger.info params.to_s
        projection_dates = @team.project.projection_dates
        Projection.delete_projections_for_team_and_bug_list(@team, @bug_list)
    
        projection_dates.each do |projection_date|
          projection = Projection.new(:team_id => @team.id, :bug_list_id => @bug_list.id, :date => projection_date, :bug_count => params[:projection][projection_date.to_s] )
          projection.save
          #logger.info "found param: " + projection_date.to_s + ":" + params[:projection][projection_date.to_s]
        end

    
        respond_to do |format|
          if true # @projection.update_attributes(params[:projection])
            flash[:notice] = 'Your projections were successfully updated.'
            #format.html { redirect_to(:controller => '/teams', :action => :show, :id => @team.id) }
            format.html { redirect_to :action => "show", :params => { :team_id => @team.id, :bug_list_id => @bug_list.id } }
            format.xml  { head :ok }
          else
            format.html { render :action => "show", :team_id => @team.id, :bug_list_id => @bug_list.id }
            format.xml  { render :xml => @projection.errors, :status => :unprocessable_entity }
          end
        end
      else
         flash[:notice] = "Stop messing with other team leads projections. It's rude."
         redirect_to( :controller => :projections, :id => @team.id, :bug_list_id => @bug_list.id, :team_id => @team.id, :action => :show )
       end 
  end

  
end
